home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Online / Qpopper / pop_user.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-24  |  3.3 KB  |  124 lines

  1. /*
  2.  * Copyright (c) 1989 Regents of the University of California.
  3.  * All rights reserved.  The Berkeley software License Agreement
  4.  * specifies the terms and conditions for redistribution.
  5.  */
  6.  
  7. #ifndef lint
  8. static char copyright[] = "Copyright (c) 1990 Regents of the University of California.\nAll rights reserved.\n";
  9. static char SccsId[] = "@(#)@(#)pop_user.c    2.1  2.1 3/18/91";
  10. #endif /* not lint */
  11.  
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14. #include <pwd.h>
  15. #include<sys/stat.h>
  16.  
  17. #if defined(SOLARIS2) || defined(SYSV) || defined(AIX)
  18. # include <string.h>
  19. # include "flock.h"
  20. # define index(s, c)    strchr(s, c)
  21. #else
  22. # include <strings.h>
  23. #endif
  24.  
  25. #if defined(SOLARIS2) || defined(UNIXWARE) || defined(AIX) || defined(PTX) \
  26.     || defined(AUX) || defined(POPSCO) || defined(OSF1) || defined(ULTRIX)
  27. # include <fcntl.h>
  28. #else
  29. # include <sys/file.h>
  30. #endif
  31.  
  32. #include <ndbm.h>
  33.  
  34. #include "popper.h"
  35.  
  36. /* 
  37.  *  user:   Prompt for the user name at the start of a POP session
  38.  */
  39.  
  40. int pop_user (p)
  41. POP     *   p;
  42. {
  43.     /* If there is an APOP database entry then don't allow a cleartext
  44.        password over the net */
  45. # ifdef APOP
  46.     char apop_dir[BUFSIZ];
  47.     DBM    *db;
  48.     int       fid;
  49.     struct passwd *pw;
  50.     struct stat st;
  51.     datum    key, value;
  52. # endif
  53.  
  54. #ifdef KERBEROS
  55.     if (p->kerberos && strcmp(p->pop_parm[1], p->user)) {
  56.     pop_log(p, LOG_WARNING, "%s: auth failed: %s.%s@@%s vs %s",
  57.         p->client, kdata.pname, kdata.pinst, kdata.prealm, 
  58.         p->pop_parm[1]);
  59.         return(pop_msg(p,POP_FAILURE,
  60.                "Wrong username supplied (%s vs. %s).", p->user,
  61.                p->pop_parm[1]));
  62.     }
  63. #endif
  64.  
  65.     /*  Save the user name */
  66.     (void)strncpy(p->user, p->pop_parm[1], sizeof(p->user));
  67.     p->user[sizeof(p->user)-1] = 0;
  68.  
  69. # ifdef APOP_ONLY
  70.     return(pop_auth_fail(p, POP_FAILURE,
  71.         "You must use APOP authentication to connect to this server"));
  72. # endif
  73.  
  74. # ifdef APOP
  75.  
  76.     /* If this call fails then the database is not accessable (doesn't
  77.        exist?) in which case we can ignore an APOP user trying to
  78.        access the popper with a cleartext password.
  79.          */
  80.     if (((pw = getpwnam(p->user)) != NULL) &&
  81.     ((db = dbm_open(APOP, O_RDONLY, 0)) != NULL)) {
  82.  
  83.     (void) strncpy(apop_dir, APOP, sizeof(apop_dir) - 5);
  84. # if defined(BSD44_DBM)
  85.     (void) strcat(apop_dir, ".db");
  86. # else
  87.     (void) strcat(apop_dir, ".dir");
  88. # endif
  89.     if (stat (apop_dir, &st) != -1 && (st.st_mode & 0777) != 0600) {
  90.         dbm_close (db);
  91.         return(pop_auth_fail(p, POP_FAILURE,
  92.         "POP authorization DB has wrong mode (0%o)",st.st_mode & 0777));
  93.     }
  94.     fid = open(apop_dir, O_RDONLY);
  95.     if(fid == -1) {
  96.         int e = errno;
  97.         dbm_close (db);
  98.         return(pop_auth_fail(p, POP_FAILURE,
  99.             "unable to lock POP authorization DB (%s)", strerror(e)));
  100.     }
  101.     if (flock (fid, LOCK_SH) == -1) {
  102.         int e = errno;
  103.         (void) close(fid);
  104.         dbm_close (db);
  105.         return(pop_auth_fail(p, POP_FAILURE,
  106.             "unable to lock POP authorization DB (%s)", strerror(e)));
  107.     }
  108.     key.dsize = strlen (key.dptr = p->user) + 1;
  109.     value = dbm_fetch (db, key);
  110.     dbm_close (db);
  111.     (void) close(fid);
  112.  
  113.     if (value.dptr != NULL) {
  114.         return(pop_auth_fail(p, POP_FAILURE,
  115.         "You must use APOP to connect to this server"));
  116.     }
  117.     }
  118. #endif /* APOP */
  119.  
  120.     /*  Tell the user that the password is required */
  121.     return (pop_msg(p,POP_SUCCESS,"Password required for %s.",p->user));
  122. }
  123.  
  124.